package com.hm.ibsm.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.common.annotation.WebClientRequestIntercept;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.ServerResponse;
import com.hm.ibsm.manage.service.UserService;
import com.hm.ibsm.manage.vo.user.UserDetailVo;
import com.hm.ibsm.manage.vo.user.UserVo;
import com.hm.ibsm.util.ControllerResult;
import com.hm.ibsm.util.R.API;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 下午11:36:19 2017年1月31日
 * @version 1.0
 * @describe 用户管理
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping(API.MODULE_NAME + "/user")
public class ApiUser {

	private static final String MODULE_TAG_NAME = "用户管理";

	@Autowired
	private UserService userService;

	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(tags = MODULE_TAG_NAME, value = "添加用户", httpMethod = "POST", notes = "添加用户,返回是否成功!")
	public ResponseEntity<ServerResponse<Boolean>> addUser(
			@ApiParam(required = true, name = "body", value = "添加用户数据结构") @RequestBody(required = true) UserVo user) {
		try {
			userService.addUser(user);
			return ControllerResult.success(true);
		} catch (Exception e) {
			log.error(MODULE_TAG_NAME, e);
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/{userId}", method = RequestMethod.PUT)
	@ApiOperation(tags = MODULE_TAG_NAME, value = "修改用户", httpMethod = "PUT", notes = "修改用户,返回是否成功!")
	public ResponseEntity<ServerResponse<Boolean>> updateUser(
			@ApiParam(required = true, name = "userId", value = "用户ID") @PathVariable("userId") String userId,
			@ApiParam(required = true, name = "body", value = "修改用户数据结构") @RequestBody(required = true) UserVo user) {
		try {
			userService.updateUser(userId, user);
			return ControllerResult.success(true);
		} catch (Exception e) {
			log.error(MODULE_TAG_NAME, e);
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/{userId}", method = RequestMethod.DELETE)
	@ApiOperation(tags = MODULE_TAG_NAME, value = "删除用户", httpMethod = "DELETE", notes = "删除用户,返回是否成功!")
	public ResponseEntity<ServerResponse<Boolean>> deleteUser(
			@ApiParam(required = true, name = "userId", value = "用户ID") @PathVariable("userId") String userId) {
		try {
			userService.deleteUser(userId);
			return ControllerResult.success(true);
		} catch (Exception e) {
			log.error(MODULE_TAG_NAME, e);
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/{userId}", method = RequestMethod.GET)
	@ApiOperation(tags = MODULE_TAG_NAME, value = "获取用户详细信息", httpMethod = "GET", notes = "获取用户详细信息")
	public ResponseEntity<ServerResponse<UserDetailVo>> getUserDetail(
			@ApiParam(required = true, name = "userId", value = "用户ID") @PathVariable("userId") String userId) {
		try {
			return ControllerResult.success(userService.getUserDetail(userId));
		} catch (Exception e) {
			log.error(MODULE_TAG_NAME, e);
			return ControllerResult.failed(e);
		}
	}

	@WebClientRequestIntercept(frequency = true)
	@RequestMapping(path = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(tags = MODULE_TAG_NAME, value = "分页查询用户信息", httpMethod = "GET", notes = "分页查询用户信息")
	public ResponseEntity<ServerResponse<PageInfo<UserVo>>> query(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,最小页码不能小于1)") @PathVariable("pageNumber") Integer pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") Integer pageSize,
			@ApiParam(required = false, name = "userName", value = "姓名") @RequestParam(name = "userName", required = false) String userName,
			@ApiParam(required = false, name = "account", value = "账户名") @RequestParam(name = "userName", required = false) String account,
			@ApiParam(required = false, name = "email", value = "邮箱") @RequestParam(name = "email", required = false) String email,
			@ApiParam(required = false, name = "mobile", value = "手机号码") @RequestParam(name = "mobile", required = false) String mobile) {
		try {

			return ControllerResult.success(userService.query(new PageInfo.PageParam(pageNumber, pageSize), userName, account, email, mobile));
		} catch (Exception e) {
			log.error(MODULE_TAG_NAME, e);
			return ControllerResult.failed(e);
		}
	}
}
